package com.qf.mvc01.controller;

import com.qf.mvc01.entity.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/json")
public class JsonController {

    /**
     * 测试返回json数据
     * http://localhost:8080/json/test1?id=1&name=zzz&gender=false&birth=2018-12-12 12:20:30
     * @param user
     * @return
     */
    @RequestMapping("/test1")
    @ResponseBody
    public User test1(User user){
        System.out.println(user);
        return user;
    }


    /**
     * 测试返回 List 类型数据，格式化成json
     * @return
     */
    @RequestMapping("/test2")
    @ResponseBody
    public List<User> test2(){
        List<User> users = new ArrayList<>();
        String[] hobby = {"football","basketball"};
        users.add(new User(1,"zhangsan",true,new Date(),null,2500.251));
        users.add(new User(2,"lisi",false,new Date(),hobby,652.125));
        return users;
    }

    /**
     * 返回字符串
     * @return
     */
    @RequestMapping(value = "/test3",produces = "text/html;charset=utf-8")  // produces属性用于防止返回的字符串中文乱码
    @ResponseBody
    public String test3(){
        System.out.println("JsonController test3");
        return "hello jackson 中文";
    }


    /**
     * 测试接收请求体中的json，并转换成java对象
     * @param user
     * @return
     */
    @RequestMapping("/test4")
    @ResponseBody
    public User test4(@RequestBody User user){
        System.out.println(user);
        return user;
    }
}
